package com.yantu.controller.student;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yantu.bean.*;
import com.yantu.mapper.ExamPaperQuestionCustomerAnswerMapper;
import com.yantu.mapper.QuestionMapper;
import com.yantu.mapper.SubjectMapper;
import com.yantu.mapper.TextContentMapper;
import com.yantu.util.Result;
import com.yantu.view.RecordPageView;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/student")
@ApiModel("错题本接口")
public class StudentWrongPageController {

    @Autowired
    ExamPaperQuestionCustomerAnswerMapper examPaperQuestionCustomerAnswerMapper;
    @Autowired
    TextContentMapper textContentMapper;
    @Autowired
    SubjectMapper subjectMapper;
    @Autowired
    QuestionMapper questionMapper;

    //试卷分页
    @GetMapping("/WrongPage")
    @ApiOperation("错题本分页")
    public Result wrongPage(@ModelAttribute() RecordPageView recordPageView) {
        PageHelper.startPage(recordPageView.getPageIndex(), recordPageView.getPageSize());

        QueryWrapper<ExamPaper> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("do_right",false);

        List<ExamPaperQuestionCustomerAnswer> examPaperQuestionCustomerAnswers = examPaperQuestionCustomerAnswerMapper.selectList(null);

        if(examPaperQuestionCustomerAnswers!=null){
            PageInfo pageInfo = new PageInfo(examPaperQuestionCustomerAnswers);
            List<ExamPaperQuestionCustomerAnswer> examPaperQuestionCustomerAnswers1 = pageInfo.getList();
            Map<String, Object> map = new HashMap<>();
            map.put("total", pageInfo.getTotal());
            List<Object> list1 = new ArrayList();
            for (ExamPaperQuestionCustomerAnswer examPaperQuestionCustomerAnswer : examPaperQuestionCustomerAnswers1) {
                Map<String, Object> map1 = new HashMap<>();
                map1.put("id",examPaperQuestionCustomerAnswer.getQuestionId());
                map1.put("questionType",examPaperQuestionCustomerAnswer.getQuestionType());

                map1.put("createTime",examPaperQuestionCustomerAnswer.getCreateTime());

                Subject subject = subjectMapper.selectById(examPaperQuestionCustomerAnswer.getSubjectId());
                map1.put("subjectName",subject.getName());
                Question question = questionMapper.selectById(examPaperQuestionCustomerAnswer.getQuestionId());


                    TextContent textContent =textContentMapper.selectById(question.getInfoTextContentId());
                    JSONObject content1 = JSONObject.fromObject(textContent.getContent());
                    map1.put("shortTitle",content1.getString("titleContent"));

                list1.add(map1);
            }
            map.put("list", list1);
            return Result.succ(200, "查询成功", map);

        }
        else return Result.succ(200, "查询不到数据", null);


    }
}
